import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { CreateOrUpdateTaskInput, CodeRuleEditDto, CodeRuleServiceProxy, ProfileServiceProxy, TaskServiceProxy, CodeRuleFormatViewDto, ThemeLayoutSettingsDto } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { ConcatSource } from 'webpack-sources';

@Component({
  selector: 'ruleFormatCreateOrEdit',
  templateUrl: './RuleFormatCreateOrEdit.component.html',
  styleUrls: ['./RuleFormatCreateOrEdit.component.css']
})
export class RuleFormatCreateOrEditComponent extends AppComponentBase {


  @ViewChild('createOrEditModal') modal: ModalDirective;
  @ViewChild('ruleFormatCreateOrEdit') ruleFormatCreateOrEdit: RuleFormatCreateOrEditComponent;
  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  isDisable = false;
  active = false;
  saving = false;

  _codeRuleFormatViewDto = [];
  itemTypeAyy = [];

  selectedItemType: any;

  selectedItemTypeID: number;
  selectedItemTypeName: string;

  itemDateAyy = [];
  selectedItemDate: string;

  itemCodeAyy = [];
  selectedItemCode: string;
  isupdate: any = false;

  codeRuleFormatViewDto: CodeRuleFormatViewDto = new CodeRuleFormatViewDto();
  constructor(injector: Injector,
    private _taskService: TaskServiceProxy,
    private _coderuleService: CodeRuleServiceProxy,
    private _profileService: ProfileServiceProxy) {
    super(injector);
  }
  changetype(): void {
    this.selectedItemTypeID = this.selectedItemType.id;
    this.selectedItemTypeName = this.selectedItemType.name;
    if (this.selectedItemType.id === 3) {
      this.isDisable = false;
    } else {
      this.isDisable = true;
    }
  }

  onShown(): void {
    this.itemTypeAyy = [
      { label: '自定义', value: { id: 1, name: '自定义' } },
      { label: '日期', value: { id: 2, name: '日期' } },
      { label: '流水号', value: { id: 3, name: '流水号' } }
    ];

    this.selectedItemTypeID = this.itemTypeAyy[0].value.id;
    this.selectedItemTypeName = this.itemTypeAyy[0].label;
    this.isDisable = true;

    this.itemDateAyy = [
      { label: 'mmdd', value: 'mmdd' },
      { label: 'ddmm', value: 'ddmm' },
      { label: 'mmyy', value: 'mmyy' },
      { label: 'yymm', value: 'yymm' },
      { label: 'yyyymm', value: 'yyyymm' },
      { label: 'yymmdd', value: 'yymmdd' },
      { label: 'yyyymmdd', value: 'yyyymmdd' }
    ];

    this.selectedItemDate = this.itemDateAyy[0].value;

    this.itemCodeAyy = [
      { label: '000', value: '000' },
      { label: '0000', value: '0000' },
      { label: '00000', value: '00000' },
      { label: '000000', value: '000000' }
    ];

    this.selectedItemCode = this.itemCodeAyy[0].value;

  }

  show(outList: any, dto?: CodeRuleFormatViewDto): void {
    if (dto != null) {
      this.selectedItemType = {id: dto.itemType, name: dto.itemTypeName};
      this.isupdate = true;
    } else {
      this.isupdate = false;
      dto = new CodeRuleFormatViewDto();
    }
    this._codeRuleFormatViewDto = outList;
    this.codeRuleFormatViewDto = dto;
    this.selectedItemTypeID = dto.itemType;
    this.selectedItemTypeName = dto.itemTypeName;
    if (dto.itemType === 1) {
      this.isDisable = true;
    }
    if (dto.itemType === 2) {
      this.selectedItemDate = dto.formatStr;
      this.isDisable = true;
    }
    if (dto.itemType === 3) {
      this.selectedItemCode = dto.formatStr;
      this.isDisable = false;
    }
    this.active = true;
    this.modal.show();

  }

  save(): void {
    if (this.selectedItemTypeID === 2) {
      this.codeRuleFormatViewDto.formatStr = this.selectedItemDate;
    } else if (this.selectedItemTypeID === 3) {
      this.codeRuleFormatViewDto.formatStr = this.selectedItemCode;
    }
    this.codeRuleFormatViewDto.itemType = this.selectedItemTypeID;
    this.codeRuleFormatViewDto.itemTypeName = this.selectedItemTypeName;
    if (!this.isupdate) {
      this._codeRuleFormatViewDto.push(this.codeRuleFormatViewDto);
    }
    this.selectedItemType = null;
    this.active = false;
    this.modal.hide();
  }
  close(): void {
    this.active = false;
    this.modal.hide();
  }


}
